<?php
class ModelDeviceDevice extends Model {
	/*
	 * getDevice
	 * 获取设备信息
	 * @param int $device_imei
	 */
	public function getDevice($device_imei){
		$sql = 'SELECT * FROM `device` WHERE `device_imei`=:device_imei AND `is_pass`=1';
		$params = array(
			':device_imei'=>$this->db->escape($device_imei)
		);
		$query = $this->db->query($sql,$params);
		if($query->num_rows>0){
			return $query->row;
		}else{
			return null;
		}
	}
	
	/*
	 * isPassDeviceAuth
	 * 设备是否通过验证
	 * @param string $device_imei 设备imei
	 * @param string $device_secret 设备私有密钥
	 * @param string $token 新生成的动态令牌
	 */
	public function isPassDeviceAuth($device_imei,$device_secret,$token){
		$sql = 'SELECT `device_id`,`is_pass` FROM  `device` WHERE `device_imei`=:device_imei AND `device_secret`=:device_secret LIMIT 1';
		$params = array(
			':device_imei'=>$this->db->escape($device_imei),
			':device_secret'=>$this->db->escape($device_secret)
		);
		$row = $this->db->query($sql,$params)->row;
		if(!empty($row) && (int)$row['is_pass']===1){
			$this->load->model('device/activity_log');
			$this->load->model('device/token');
			$this->model_device_activity_log->addActivityLog($row['device_id'],'auth'); //添加设备验证记录
			$this->model_device_token->updateToken($row['device_id'],$token); //更新数据库的令牌
			return true;
		}else{
			return false;
		}
	}
}